/*******************************************************************************
 * Copyright (c) 2005, 2014 springside.github.io
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 *******************************************************************************/
package net.wofly.sale.stock.repository;

import net.wofly.sale.report.domain.OutStockDTO;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;

import net.wofly.sale.stock.domain.OutStock;
import org.springframework.data.repository.query.Param;

import java.util.List;

public interface OutStockRepository
		extends PagingAndSortingRepository<OutStock, String>, JpaSpecificationExecutor<OutStock> {
	
	OutStock findByProofID(String proofID);

	/**
	 * 查询销售数量
	 *
	 * @param countDate
	 * @param countEndTime
     * @return
     */
	@Query(value="select new OutStockDTO(u.userID, u.name, iosd.commodityTerminalID, ct.commodityTerminalName, sum(iosd.num)) " +
			"from OutStock os, User u, InOutStockDetail iosd, CommodityTerminal ct " +
			"where os.modifyDate between :countDate and :countEndTime and os.outStockType = 1" +
			"and os.warehouseID = u.warehouseID and os.outStockID = iosd.operateStockID and iosd.commodityTerminalID = ct.commodityTerminalID " +
			"group by u.userID, u.name, iosd.commodityTerminalID, ct.commodityTerminalName")
	List<OutStockDTO> findByCountDate(@Param("countDate") Long countDate, @Param("countEndTime") Long countEndTime);
}